package com.chuangshi.core.vote.admin.service.impl;

import com.chuangshi.core.kit.Result;
import com.chuangshi.core.kit.Column;
import com.chuangshi.core.mongodb.MongodbServiceImpl;
import com.chuangshi.core.vote.admin.controller.admin.request.AdminAdminV1ListRequest;
import com.chuangshi.core.vote.admin.entity.Admin;
import com.chuangshi.core.vote.admin.entity.column.AdminColumn;
import com.chuangshi.core.vote.admin.service.AdminService;
import cn.hutool.core.util.StrUtil;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 管理员信息业务接口实现
 *
 * @author ggliming
 * @date 2019-02-21
 */
@Service
public class AdminServiceImpl extends MongodbServiceImpl<Admin> implements AdminService {

    @Override
    public Result listForAdmin(AdminAdminV1ListRequest request) {
        Criteria criteria = Criteria.where(Column.APP_ID).is(request.getAppId())
                        .and(Column.SYSTEM_STATUS).is(true);
        if (StrUtil.isNotBlank(request.getAdminId())) {
            criteria.and(AdminColumn.ADMIN_ID).regex(".*?" + request.getAdminId() + ".*");
        }

        List<Sort.Order> orders = new ArrayList<Sort.Order>();
        orders.add(new Sort.Order(Sort.Direction.DESC, AdminColumn.ADMIN_ID));
        Sort sort = Sort.by(orders);

        Integer total = count(criteria);
        List<Admin> list = list(criteria, sort, request.getPageIndex(), request.getPageSize());


        Result result = new Result();
        result.setTotal(total);
        result.setList(list);

        return result;
    }

    @Override
    public Admin findByAccount(String adminAccount) {
        Criteria criteria = Criteria.where(AdminColumn.ADMIN_ACCOUNT).is(adminAccount)
                .and(Column.SYSTEM_STATUS).is(true);
        return find(criteria);
    }
}